<template>
  <Dialog :title="dialogTitle" v-model="dialogVisible">
    <el-form
      ref="formRef"
      :model="formData"
      :rules="formRules"
      label-width="100px"
      v-loading="formLoading"
    >
      <el-form-item label="父级编码" prop="parentCode">
        <el-input v-model="formData.parentCode" placeholder="请输入父级编码" />
      </el-form-item>
      <el-form-item label="所有父级编码" prop="parentCodes">
        <el-input v-model="formData.parentCodes" placeholder="请输入所有父级编码" />
      </el-form-item>
      <el-form-item label="本级排序号" prop="treeSort">
        <el-input v-model="formData.treeSort" placeholder="请输入本级排序号" />
      </el-form-item>
      <el-form-item label="所有级别排序号" prop="treeSorts">
        <el-input v-model="formData.treeSorts" placeholder="请输入所有级别排序号" />
      </el-form-item>
      <el-form-item label="是否最末级（1是  0否）" prop="treeLeaf">
        <el-input v-model="formData.treeLeaf" placeholder="请输入是否最末级（1是  0否）" />
      </el-form-item>
      <el-form-item label="层次级别" prop="treeLevel">
        <el-input v-model="formData.treeLevel" placeholder="请输入层次级别" />
      </el-form-item>
      <el-form-item label="全节点名" prop="treeNames">
        <el-input v-model="formData.treeNames" placeholder="请输入全节点名" />
      </el-form-item>
      <el-form-item label="公司代码" prop="viewCode">
        <el-input v-model="formData.viewCode" placeholder="请输入公司代码" />
      </el-form-item>
      <el-form-item label="公司名称" prop="companyName">
        <el-input v-model="formData.companyName" placeholder="请输入公司名称" />
      </el-form-item>
      <el-form-item label="公司全称" prop="fullName">
        <el-input v-model="formData.fullName" placeholder="请输入公司全称" />
      </el-form-item>
      <el-form-item label="区域编码" prop="areaCode">
        <el-input v-model="formData.areaCode" placeholder="请输入区域编码" />
      </el-form-item>
      <el-form-item label="所在区域" prop="localAreaCode">
        <el-input v-model="formData.localAreaCode" placeholder="请输入所在区域" />
      </el-form-item>
      <el-form-item label="行业类型（字典industry_type）" prop="industryType">
        <el-select v-model="formData.industryType" placeholder="请选择行业类型（字典industry_type）">
          <el-option label="请选择字典生成" value="" />
        </el-select>
      </el-form-item>
      <el-form-item label="行业部门" prop="industryDept">
        <el-input v-model="formData.industryDept" placeholder="请输入行业部门" />
      </el-form-item>
      <el-form-item label="公司类型（字典company_type）" prop="companyType">
        <el-select v-model="formData.companyType" placeholder="请选择公司类型（字典company_type）">
          <el-option label="请选择字典生成" value="" />
        </el-select>
      </el-form-item>
      <el-form-item label="监管类别（字典unit_monitor_type）" prop="unitType">
        <el-select v-model="formData.unitType" placeholder="请选择监管类别（字典unit_monitor_type）">
          <el-option label="请选择字典生成" value="" />
        </el-select>
      </el-form-item>
      <el-form-item label="经度" prop="lng">
        <el-input v-model="formData.lng" placeholder="请输入经度" />
      </el-form-item>
      <el-form-item label="纬度" prop="lat">
        <el-input v-model="formData.lat" placeholder="请输入纬度" />
      </el-form-item>
      <el-form-item label="对接app_id" prop="appId">
        <el-input v-model="formData.appId" placeholder="请输入对接app_id" />
      </el-form-item>
      <el-form-item label="第三方数据id" prop="dataId">
        <el-input v-model="formData.dataId" placeholder="请输入第三方数据id" />
      </el-form-item>
      <el-form-item label="省id" prop="provId">
        <el-input v-model="formData.provId" placeholder="请输入省id" />
      </el-form-item>
      <el-form-item label="市id" prop="cityId">
        <el-input v-model="formData.cityId" placeholder="请输入市id" />
      </el-form-item>
      <el-form-item label="区县id" prop="countyId">
        <el-input v-model="formData.countyId" placeholder="请输入区县id" />
      </el-form-item>
      <el-form-item label="乡镇街道id" prop="townId">
        <el-input v-model="formData.townId" placeholder="请输入乡镇街道id" />
      </el-form-item>
      <el-form-item label="村id" prop="villageId">
        <el-input v-model="formData.villageId" placeholder="请输入村id" />
      </el-form-item>
      <el-form-item label="网格id" prop="gridId">
        <el-input v-model="formData.gridId" placeholder="请输入网格id" />
      </el-form-item>
      <el-form-item label="状态" prop="status">
        <el-radio-group v-model="formData.status">
          <el-radio value="1">请选择字典生成</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="创建人" prop="createUser">
        <el-input v-model="formData.createUser" placeholder="请输入创建人" />
      </el-form-item>
      <el-form-item label="修改人" prop="updateUser">
        <el-input v-model="formData.updateUser" placeholder="请输入修改人" />
      </el-form-item>
      <el-form-item label="备注" prop="remarks">
        <el-input v-model="formData.remarks" placeholder="请输入备注" />
      </el-form-item>
      <el-form-item label="省" prop="provName">
        <el-input v-model="formData.provName" placeholder="请输入省" />
      </el-form-item>
      <el-form-item label="市" prop="cityName">
        <el-input v-model="formData.cityName" placeholder="请输入市" />
      </el-form-item>
      <el-form-item label="区县" prop="countyName">
        <el-input v-model="formData.countyName" placeholder="请输入区县" />
      </el-form-item>
      <el-form-item label="乡镇街道" prop="townName">
        <el-input v-model="formData.townName" placeholder="请输入乡镇街道" />
      </el-form-item>
      <el-form-item label="村" prop="villageName">
        <el-input v-model="formData.villageName" placeholder="请输入村" />
      </el-form-item>
      <el-form-item label="网格" prop="gridName">
        <el-input v-model="formData.gridName" placeholder="请输入网格" />
      </el-form-item>
      <el-form-item label="联网状态（0未联网1已联网）" prop="networkStatus">
        <el-radio-group v-model="formData.networkStatus">
          <el-radio value="1">请选择字典生成</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="消防安全责任人" prop="fireSafePerson">
        <el-input v-model="formData.fireSafePerson" placeholder="请输入消防安全责任人" />
      </el-form-item>
      <el-form-item label="消防安全责任人电话" prop="fireSafePersonTel">
        <el-input v-model="formData.fireSafePersonTel" placeholder="请输入消防安全责任人电话" />
      </el-form-item>
      <el-form-item label="组织机构代码" prop="orgLicense">
        <el-input v-model="formData.orgLicense" placeholder="请输入组织机构代码" />
      </el-form-item>
      <el-form-item label="地址" prop="address">
        <el-input v-model="formData.address" placeholder="请输入地址" />
      </el-form-item>
    </el-form>
    <template #footer>
      <el-button @click="submitForm" type="primary" :disabled="formLoading">确 定</el-button>
      <el-button @click="dialogVisible = false">取 消</el-button>
    </template>
  </Dialog>
</template>
<script setup lang="ts">
import { SysCompanyApi, SysCompany } from '@/api/cfs/company'

/** sys_company 表单 */
defineOptions({ name: 'SysCompanyForm' })

const { t } = useI18n() // 国际化
const message = useMessage() // 消息弹窗

const dialogVisible = ref(false) // 弹窗的是否展示
const dialogTitle = ref('') // 弹窗的标题
const formLoading = ref(false) // 表单的加载中：1）修改时的数据加载；2）提交的按钮禁用
const formType = ref('') // 表单的类型：create - 新增；update - 修改
const formData = ref({
  companyCode: undefined,
  parentCode: undefined,
  parentCodes: undefined,
  treeSort: undefined,
  treeSorts: undefined,
  treeLeaf: undefined,
  treeLevel: undefined,
  treeNames: undefined,
  viewCode: undefined,
  companyName: undefined,
  fullName: undefined,
  areaCode: undefined,
  localAreaCode: undefined,
  industryType: undefined,
  industryDept: undefined,
  companyType: undefined,
  unitType: undefined,
  lng: undefined,
  lat: undefined,
  appId: undefined,
  dataId: undefined,
  provId: undefined,
  cityId: undefined,
  countyId: undefined,
  townId: undefined,
  villageId: undefined,
  gridId: undefined,
  status: undefined,
  createUser: undefined,
  updateUser: undefined,
  remarks: undefined,
  provName: undefined,
  cityName: undefined,
  countyName: undefined,
  townName: undefined,
  villageName: undefined,
  gridName: undefined,
  networkStatus: undefined,
  fireSafePerson: undefined,
  fireSafePersonTel: undefined,
  orgLicense: undefined,
  address: undefined,
})
const formRules = reactive({
})
const formRef = ref() // 表单 Ref

/** 打开弹窗 */
const open = async (type: string, id?: number) => {
  dialogVisible.value = true
  dialogTitle.value = t('action.' + type)
  formType.value = type
  resetForm()
  // 修改时，设置数据
  if (id) {
    formLoading.value = true
    try {
      formData.value = await SysCompanyApi.getSysCompany(id)
    } finally {
      formLoading.value = false
    }
  }
}
defineExpose({ open }) // 提供 open 方法，用于打开弹窗

/** 提交表单 */
const emit = defineEmits(['success']) // 定义 success 事件，用于操作成功后的回调
const submitForm = async () => {
  // 校验表单
  await formRef.value.validate()
  // 提交请求
  formLoading.value = true
  try {
    const data = formData.value as unknown as SysCompany
    if (formType.value === 'create') {
      await SysCompanyApi.createSysCompany(data)
      message.success(t('common.createSuccess'))
    } else {
      await SysCompanyApi.updateSysCompany(data)
      message.success(t('common.updateSuccess'))
    }
    dialogVisible.value = false
    // 发送操作成功的事件
    emit('success')
  } finally {
    formLoading.value = false
  }
}

/** 重置表单 */
const resetForm = () => {
  formData.value = {
    companyCode: undefined,
    parentCode: undefined,
    parentCodes: undefined,
    treeSort: undefined,
    treeSorts: undefined,
    treeLeaf: undefined,
    treeLevel: undefined,
    treeNames: undefined,
    viewCode: undefined,
    companyName: undefined,
    fullName: undefined,
    areaCode: undefined,
    localAreaCode: undefined,
    industryType: undefined,
    industryDept: undefined,
    companyType: undefined,
    unitType: undefined,
    lng: undefined,
    lat: undefined,
    appId: undefined,
    dataId: undefined,
    provId: undefined,
    cityId: undefined,
    countyId: undefined,
    townId: undefined,
    villageId: undefined,
    gridId: undefined,
    status: undefined,
    createUser: undefined,
    updateUser: undefined,
    remarks: undefined,
    provName: undefined,
    cityName: undefined,
    countyName: undefined,
    townName: undefined,
    villageName: undefined,
    gridName: undefined,
    networkStatus: undefined,
    fireSafePerson: undefined,
    fireSafePersonTel: undefined,
    orgLicense: undefined,
    address: undefined,
  }
  formRef.value?.resetFields()
}
</script>